<template>
  <!-- 列表 -->
  <ContentWrap>
<!--    <el-button-->
<!--      type="primary"-->
<!--      plain-->
<!--      @click="openForm('create')"-->
<!--      v-hasPermi="['mes:base-work-plan:create']"-->
<!--    >-->
<!--      <Icon icon="ep:plus" class="mr-5px" /> 新增-->
<!--    </el-button>-->
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
<!--      <el-table-column label="投料单详细信息ID" align="center" prop="id" />-->
<!--      <el-table-column label="任务单号" align="center" prop="workPlanId" />-->
      <el-table-column label="投料单编号" align="center" prop="bomId" min-width="170"/>
      <el-table-column label="投料日期" align="center" prop="bomDate"  min-width="120"   :formatter="dateFormatter2" />

<!--      <el-table-column label="投料单内码" align="center" prop="bomInterId" />-->
      <el-table-column label="制单人" align="center" prop="biller" />
      <el-table-column label="物料名称" align="center" prop="materialName" min-width="200"/>
      <el-table-column label="物料代码" align="center" prop="materialId" min-width="150"/>
      <el-table-column label="仓库代码" align="center" prop="stockId" />
      <el-table-column label="仓库名称" align="center" prop="stockName" min-width="150"/>
<!--      <el-table-column label="计量单位编号" align="center" prop="unitId" />-->

      <el-table-column label="单位用量" align="center" prop="unitNum" />
      <el-table-column label="标准用量" align="center" prop="standardNum" />
      <el-table-column label="应领数量" align="center" prop="shouldGetNum" />
      <el-table-column label="已领数量" align="center" prop="hasGetNum" />
      <el-table-column label="单位名称" align="center" prop="unitName" />
      <el-table-column label="变更次数" align="center" prop="changeTimes" />
      <el-table-column label="行数" align="center" prop="entryId" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column
        label="更新时间"
        align="center"
        prop="updateTime"
        :formatter="dateFormatter"
        width="180px"
      />

       <el-table-column label="备注" align="center" prop="note" />
<!--      <el-table-column label="操作" align="center">-->
<!--        <template #default="scope">-->
<!--          <el-button-->
<!--            link-->
<!--            type="primary"-->
<!--            @click="openForm('update', scope.row.id)"-->
<!--            v-hasPermi="['mes:base-work-plan:update']"-->
<!--          >-->
<!--            编辑-->
<!--          </el-button>-->
<!--          <el-button-->
<!--            link-->
<!--            type="danger"-->
<!--            @click="handleDelete(scope.row.id)"-->
<!--            v-hasPermi="['mes:base-work-plan:delete']"-->
<!--          >-->
<!--            删除-->
<!--          </el-button>-->
<!--        </template>-->
<!--      </el-table-column>-->
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      :small="true"
      @pagination="getList"
    />
  </ContentWrap>
    <!-- 表单弹窗：添加/修改 -->
    <BaseBomChildForm ref="formRef" @success="getList" />
</template>
<script setup lang="ts">
import {dateFormatter, dateFormatter2} from '@/utils/formatTime'
import { BaseWorkPlanApi } from '@/api/mes/baseworkplan'
import BaseBomChildForm from './BaseBomChildForm.vue'

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const props = defineProps<{
  workPlanInterId?: string // 投料单内码（主表的关联字段）
}>()
const loading = ref(false) // 列表的加载中
const list = ref([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  workPlanInterId: undefined as unknown
})

/** 监听主表的关联字段的变化，加载对应的子表数据 */
watch(
  () => props.workPlanInterId,
  (val: string) => {
    if (!val) {
      return
    }
    console.log("InterId:",props.workPlanInterId)
    queryParams.workPlanInterId = val
    handleQuery()
  },
    { immediate: true, deep: true }
)

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await BaseWorkPlanApi.getBaseBomChildPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  if (!props.workPlanInterId) {
    message.error('请选择一个任务单信息')
    return
  }
  formRef.value.open(type, id, props.workPlanInterId)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await BaseWorkPlanApi.deleteBaseBomChild(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}
</script>
